package com.veo.action;

import com.opensymphony.xwork2.ActionSupport;
import com.veo.entity.Books;
import com.veo.entity.BorrowBooks;
import com.veo.service.BookService;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("getBooksAction")
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/")
@Results({
        @Result(name = "success",type = "json",params = {"root","resultMapToBooks"}),
        @Result(name = "ok",type = "json",params = {"root","resultMapToBorrowBooks"}),
})
public class GetBooks extends ActionSupport {

    @Autowired
    private BookService bookService;

    private List<Books> resultMapToBooks;
    private List<BorrowBooks> resultMapToBorrowBooks;

    public List<Books> getResultMapToBooks() {
        return resultMapToBooks;
    }

    public void setResultMapToBooks(List<Books> resultMapToBooks) {
        this.resultMapToBooks = resultMapToBooks;
    }

    public List<BorrowBooks> getResultMapToBorrowBooks() {
        return resultMapToBorrowBooks;
    }

    public void setResultMapToBorrowBooks(List<BorrowBooks> resultMapToBorrowBooks) {
        this.resultMapToBorrowBooks = resultMapToBorrowBooks;
    }

    @Action("getAllBooks")
    public String getAllBooks(){
        resultMapToBooks = bookService.getAllBooksToTable();
        return  "success";
    }

    @Action("getBorrowBooks")
    public String getBorrowBooks(){
        String id = ServletActionContext.getRequest().getParameter("id");
        resultMapToBorrowBooks = bookService.getUserBorrowBooks(id);
        return  "ok";
    }



}
